package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.bean.Product;
import com.example.demo.bean.QueryInfo;
import com.example.demo.bean.User;
import com.example.demo.dao.UserDao;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
public class Usercontroller {
    @Autowired
    private UserDao userDao;
    @RequestMapping("/alluser")
    public String getUserList(QueryInfo queryInfo){
        //查询个数
       int numbers=userDao.getUserCounts("%"+queryInfo.getQuery()+"%");
       //偏移量
        int pageStart =(queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        //数据：用户、偏移量、页最大数
        List<User> users=userDao.getAllUser("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());

        HashMap<String,Object> res=new HashMap<>();
        if (users!=null){
            res.put("users",users);
            res.put("numbers",numbers);
            res.put("pageNum", queryInfo.getPageNum());
        }
        String res_user= JSON.toJSONString(res);
        return  res_user;

    }


    // 修改接口
    @PostMapping("/updatestate")
    public String upDateState(@RequestBody User dto) {
        int i = userDao.upDatestate(dto.getId(), dto.getState());
        return i > 0 ? "success" : "error";
    }
    @PostMapping("/updateimgpath")
    public String upDateImgpath(@RequestBody User dto){
        int i=userDao.upDateimgpath(dto.getId(),dto.getImgpath());
        return i > 0 ? "success" : "error";
    }
    @PostMapping("/updateuser")
    public  String upDateUser(@RequestBody User dto){
        int i=userDao.upDateUser(dto.getId(),dto.getRole(),dto.getEmail());
        return i > 0 ? "success" : "error";
    }
}
